package com.learn.project2.utils;

import org.omg.CORBA.INTERNAL;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.util.Date;

/**
 * \* Author: Valley
 * \* Date: 2022/1/11
 * \* Time: 20:36
 * \* Description:
 */

public class Md5Utils {

    private static String getMd5(String content) throws Exception{
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] bytes = content.getBytes();
        byte[] code = digest.digest(bytes);
        StringBuffer sb = new StringBuffer();
        for (byte b : code) {
            int temp = b&0xff;
            sb.append(Integer.toHexString(temp));
        }
        return sb.toString();
    }

    private static String getMd5(String content, String salt) throws Exception {
        return getMd5(content+"1999-09-22"+salt);
    }

    public static String MultiMd5(String content) throws Exception {
        return getMd5(getMd5(content,"project2"),"hello java");
    }

    public static String getFile(File file) throws Exception{
        MessageDigest digest = MessageDigest.getInstance("md5");
        FileInputStream fis = new FileInputStream(file);
        int readCount = 0;
        byte[] bytes = new byte[1024];
        while ((readCount = fis.read(bytes))!=-1){
            digest.update((byte) readCount);
        }
        byte[] result = digest.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : result) {
            sb.append(Integer.toHexString(b&0xff));
        }
        return sb.toString();
    }
}
